'''
Created on Apr 24, 2010

@author: Lukasz Spintzyk
'''
import unittest

import os, time

from fsutils.utils import mount_filesystem, create_filesystem,\
    umount_filesystem
from asyncore import write


class TestCRUDOperations(unittest.TestCase):

    def setUp(self):
        key = r'AA::22::33::44::55::66::77::88::01::22::33::44::55::66::77::88'
        block_size = 1024
        blocks_per_group = 245
        inodes_per_group = 240
        groups_count = 1000
        
        
        device = os.path.join(os.getcwd() ,"../../tst_py.fs")
        self.mount_point = os.path.join(os.getcwd(), "../../mount_point")
        if not os.path.exists(self.mount_point):
            os.mkdir(self.mount_point)
            
        open(device, "w").close()
        
        create_filesystem(device, key, groups_count, block_size, blocks_per_group, inodes_per_group)
        
        mount_filesystem(self.mount_point, key, device)
        self.dir = os.getcwd()
        time.sleep(0.1)
        os.chdir(self.mount_point)


    def tearDown(self):
        os.chdir(self.dir)
        time.sleep(0.1)
        umount_filesystem(self.mount_point)
        time.sleep(0.1)
        os.chdir(self.mount_point)
        os.system("rm -rf *")
        os.chdir(self.dir)
        pass


    def testCreateDir(self):
        print "testCreateDir"
        os.makedirs("kat1/kat11")
        dirs = os.listdir('.');
        assert(dirs[0] == "kat1")
        assert(len(dirs) == 1)
        
        os.makedirs("kat2/kat21")
        dirs = os.listdir('.');
        assert(dirs[1] == "kat2")
        assert(len(dirs) == 2)

        os.makedirs("kat3")
        dirs = os.listdir('.');
        assert(dirs[2] == "kat3")
        assert(len(dirs) == 3)
        
        os.makedirs("kat1/kat11/kat111")
        dirs = os.listdir('.');
        assert(dirs[0] == "kat1")
        assert(len(dirs) == 3)
        
        dirs = os.listdir("./kat1")
        assert(dirs[0] == "kat11")
        assert(len(dirs) == 1)
        
        dirs = os.listdir("./kat1/kat11")
        assert(dirs[0] == "kat111")
        assert(len(dirs) == 1)
        
        dirs = os.listdir('./kat2');
        assert(dirs[0] == "kat21")
        assert(len(dirs) == 1)
        
        pass

    def testRenameDir(self):
        print "testRenameDir"
        os.makedirs("kat1/kat11")
        os.makedirs("kat2")
        os.makedirs("nowy_moj_katalog")
        os.rename("nowy_moj_katalog", "nowy_moj_katalog_renamed")
        
        dirs = os.listdir(".")
        assert(dirs[2] == "nowy_moj_katalog_renamed")
        assert(len(dirs) == 3)
        
        for dir in dirs:
            assert(dir != "nowy_moj_katalog")
        
        dirs = os.listdir(".")
        os.rename("kat2", "kat1/kat12")
        dirs = os.listdir(".")
        for dir in dirs:
            assert(dir != "kat2")
        assert(len(dirs) == 2)
        
        
        dirs = os.listdir("kat1")
        assert(dirs[1] == "kat12")
        assert(len(dirs) == 2)
        pass

    def testRemoveDir(self):
        print "testRemoveDir"
        os.makedirs("kat1/kat11")
        os.makedirs("kat2")
        
        os.rmdir("kat1/kat11")
        dirs = os.listdir("kat1")
        assert(len(dirs) == 0 )
        
        dirs = os.listdir(".")
        assert(len(dirs) == 2)
        assert(dirs[0] == "kat1")
        assert(dirs[1] == "kat2")
        
        os.rmdir("kat2")
        dirs = os.listdir(".")
        assert(len(dirs) == 1)
        assert(dirs[0] == "kat1")
        
        os.rmdir("kat1")
        dirs = os.listdir(".")
        assert(len(dirs) == 0)
        
        os.makedirs("kat3/kat33")
        os.removedirs("kat3")
        dirs = os.listdir(".")
        assert(len(dirs) == 0)
        
        os.makedirs("kat3")
        dirs = os.listdir(".")
        assert(dirs[0] == "kat3")
        assert(len(dirs) == 1)
        
        dirs = os.listdir("kat3")
        assert(len(dirs) == 0)
        
    def testCopyDirs(self):
        print "testCopyDirs"
        os.makedirs("kat1/kat11")
        os.makedirs("kat2")
        
        #shutil.copytree(os.path.join(self.mount_point, "kat1"), os.path.join(self.mount_point, "kat2/"))
        os.system("cp -r kat1 kat2/")

        dirs = os.listdir(".")
        assert(dirs[0]=="kat1")
        assert(dirs[1]=="kat2")
        assert(len(dirs) == 2)
        
        dirs = os.listdir("kat2")
        assert(len(dirs) == 1)
        assert(dirs[0] == "kat1")
        dirs = os.listdir("kat2/kat1")
        assert(len(dirs) == 1)
        assert(dirs[0] == "kat11")
        
        dirs = os.listdir("kat2/kat1/kat11")
        assert(len(dirs) == 0)
        
    def testCreateFile(self):
        print "testCreateFile"
        fp = open("plik", r'w')
        fp.close()
        
        files = os.listdir(".")
        assert(files[0] == "plik")
        assert(len(files) == 1)

        assert(os.path.isfile("plik"))
        
    def testOpenFileForRead(self):
        print "testOpenFileForRead"
        fp = open("plik", 'w')
        s = "ala ma kota kot ma ale"
        fp.write(s)
        fp.close()
        
        stats = os.stat("plik")
        assert(stats.st_size == len(s))
        fp = open("plik", 'r')
        lines = fp.readlines()
        assert(lines[0] == s)
        assert(len(lines) == 1)
        
        
